import Http from '@/utils/Http';
import Constant, { Uri } from '@/constant/Constants';
import { QrcodeRequest } from '@/protocol/request/QrcodeRequest';
import { QrcodeResponse } from '@/protocol/response/QrcodeResponse';
import Util from '@/utils/Util';
import UniUtil from '@/utils/UniUtil';
import ProtocolResponse from '@/protocol/response/ProtocolResponse';

export default class QrcodeService{

    public static async  queryQrcodeList(request:QrcodeRequest.QueryList){
        return await Http.request(Uri.qrcode_list_query,request,new Array<QrcodeResponse.QrocdeInfo>());
    }

    public static async  queryUserQrcodeList(request:QrcodeRequest.QueryList){
        return await Http.request(Uri.qrcode_list_query_me,request,new QrcodeResponse.UserQrocde());
    }

    public static async  queryQrcodeUrl(id:number){
        return await Http.request(Uri.qrcode_url_query,{id:id},new QrcodeResponse.QrocdeInfo(),true,false,false);
    }

    public static async  queryHomeQrcodeUrl(id:number):Promise<ProtocolResponse<QrcodeResponse.QrocdeInfo>>{
        return await Http.request(Uri.qrcode_url_query,{id:id},new ProtocolResponse(),true,false,false);
    }

    public static async  uploadQrcode(request:QrcodeRequest.UploadGroup,url:string){
        let param = new QrcodeRequest.UploadGroup();
        Util.copy(param,request);
        param.qrcode = url;
        let res = await Http.request(Uri.qrcode_qrcode_upload,param,new QrcodeResponse.UploadGroup(),true,true);
        if (!Util.isEmpty(res)) {
            request.title = res.title;
            if (!Util.isEmpty(res.id)) {
                request.id = res.id;
            }
            request.picurl = res.picurl;
            request.qrcode = url;
            request.expireDate = res.expireDate;
        }
    }

    public static async  downloadQrcode(request:QrcodeRequest.Download){
        return  Http.request(Uri.qrcode_qrcode_download,request,new QrcodeResponse.Download(),true,false,false);
    }

    public static async downloadQrocdeCost(){
        return  Http.request(Uri.qrcode_qrcode_download_cost,null,new Array<QrcodeResponse.DownloadCost>(),true,false,true);
    }

    public static async  uploadMember(request:QrcodeRequest.UploadGroup,url:string){
        let param = new QrcodeRequest.UploadGroup();
        Util.copy(param,request);
        param.member = url;
        let res =  await Http.request(Uri.qrcode_member_upload,param,new QrcodeResponse.UploadGroup(),true,true);
        if (!Util.isEmpty(res)) {
            request.peoples = res.peoples;
            if (!Util.isEmpty(res.id)) {
                request.id = res.id;
            }
            request.picurl2 = res.picurl;
            request.member = url;
        }
    }

    public static async  submitGroup(request:QrcodeRequest.UploadGroup):Promise<ProtocolResponse<any>>{
        if(Util.isEmpty(request.qrcode)){
            UniUtil.showError({title:"二维码为空"});
            return null;
        }
        if(Util.isEmpty(request.member)){
            UniUtil.showError({title:"群人数为空"});
            return null;
        }
        if(Util.isEmpty(request.title)){
            UniUtil.showError({title:"群名为空"});
            return null;
        }
        if(Util.isEmpty(request.classIds)){
            UniUtil.showError({title:"分类为空"});
            return null;
        }
        return await Http.request(Uri.qrcode_group_submit,request,new ProtocolResponse(),true,true);
    }

    public static async  queryClass(){
        return await Http.request(Uri.qrcode_class_query,null,new Array<QrcodeResponse.QueryClass>());
    }


    public static async  top(request:QrcodeRequest.TopGroup){
       return  await Http.request(Uri.qrcode_qrcode_top,request,new QrcodeResponse.UserQrocdeInfo(),true,true);
    }

    public static async  refresh(request:QrcodeRequest.RefreshGroup){
        return  await Http.request(Uri.qrcode_qrcode_refresh,request,new QrcodeResponse.UserQrocdeInfo,true,true);
    }

}